package com.caigou.controller.front;

import com.caigou.bean.cms.entity.*;
import com.caigou.bean.cms.param.ContributeParam;
import com.caigou.bean.cms.param.ContributeQueryParam;
import com.caigou.bean.cms.param.MyParam;
import com.caigou.bean.exam.entity.Users;
import com.caigou.enums.ResponseEnum;
import com.caigou.security.utils.CurrentUser;
import com.caigou.service.*;
import com.caigou.util.DateUtil;
import com.caigou.util.Result;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/front/my/")
public class FrontMyController {

    @Resource
    private ArticleService articleService;
    @Resource
    private ICommentService commentService;
    @Resource
    private SystemMessageService systemMessageService;
    @Resource
    private ContributeService contributeService;
    @Resource
    private LessonService lessonService;

    /**
     * 获取“我的收藏”中的文章列表，支持分页
     *
     * @param myParam
     * @param user
     * @return
     */
    @RequestMapping("listMyCollectionArticle")
    public Result listMyCollectionArticle(@RequestBody MyParam myParam, @CurrentUser Users user) {
        String userId = user.getUserid();
        if (userId == null) {
            return Result.error("登录用户id不能为空", "登录用户id不能为空");
        }
        if (myParam.getPageNum() == null) {
            myParam.setPageNum(1);
        }
        if (myParam.getPageSize() == 10) {
            myParam.setPageSize(10);
        }
        return articleService.listMyCollectionArticle(userId, myParam.getPageNum(), myParam.getPageSize());
    }

    /**
     * 获取“我的评论”中的评论列表，支持分页
     *
     * @param param
     * @param user
     * @return
     */
    @RequestMapping("listMyComment")
    public Result listMyComment(@RequestBody MyParam param, @CurrentUser Users user) {
        String userId = user.getUserid();
        if (userId == null) {
            return Result.error("登录用户id不能为空", "登录用户id不能为空");
        }
        if (param.getPageNum() == null) {
            param.setPageNum(1);
        }
        if (param.getPageSize() == 10) {
            param.setPageSize(10);
        }
        return commentService.listMyComment(userId, param.getPageNum(), param.getPageSize());
    }

    @RequestMapping("listMyMessage")
    public Result listMyMessage(@RequestBody MyParam param, @CurrentUser Users user) {
        String userId = user.getUserid();
        if (userId == null) {
            return Result.error("登录用户id不能为空", "登录用户id不能为空");
        }
        if (param.getPageNum() == null) {
            param.setPageNum(1);
        }
        if (param.getPageSize() == 10) {
            param.setPageSize(10);
        }
        PageInfo<SystemMessage> pageInfo = systemMessageService.listMyMessage(userId, param.getPageNum(), param.getPageSize());
        return Result.success(pageInfo);
    }

    @RequestMapping("getAndReadSystemMessage")
    public Result getAndReadSystemMessage(@RequestBody MyParam param, @CurrentUser Users user) {
        String userId = user.getUserid();
        if (userId == null) {
            return Result.error("登录用户id不能为空", "登录用户id不能为空");
        }
        if (param.getMessageId() == null) {
            return Result.error("消息id参数缺失");
        }
        SystemMessage message = systemMessageService.getAndReadSystemMessage(userId, param.getMessageId());
        return Result.success(message);
    }

    @RequestMapping("saveContribute")
    public Result saveContribute(@RequestParam(value = "files", required = false) MultipartFile[] files, @RequestParam(value = "contributor", required = false) String contributor,
                                 @RequestParam(value = "tel", required = false) String tel, @RequestParam(value = "email", required = false) String email, @RequestParam(value = "contact", required = false) String contact,
                                 @RequestParam(value = "postcode", required = false) String postcode, @RequestParam(value = "title", required = false) String title, @RequestParam(value = "type", required = false) Integer type,
                                 @RequestParam(value = "channelId", required = false) Integer channelId, @RequestParam(value = "note", required = false) String note,
                                 @RequestParam(value = "publishType", required = false) Integer publishType, @RequestParam(value = "relativeUrl", required = false) String relativeUrl,
                                 @RequestParam(value = "prepublishDate", required = false) String prepublishDate, @RequestParam(value = "company", required = false) String company,
                                 @CurrentUser Users user) {
        String userId = user.getUserid();
        if (userId == null) {
            return Result.error("登录用户id不能为空", "登录用户id不能为空");
        }
        if (title == null || title.trim().length() < 1) {
            return Result.error("稿件标题不能为空");
        }
        title = title.trim();
        if(title.length()>50){
            return Result.error("标题不能超过50个字");
        }
        if (contributor == null || contributor.trim().length() < 1) {
            return Result.error("联系人不能为空");
        }
        contributor = contributor.trim();
        if(contributor.length()>16){
            return Result.error("联系人不能超过16个字");
        }
        if (files == null || files.length < 1) {
            return Result.error("投稿附件不能为空");
        }
        if (tel == null || tel.trim().length() < 1) {
            return Result.error("联系电话不能为空");
        }
        tel = tel.trim();
        if(tel.length()>30){
            return Result.error("联系电话不能超过30个字");
        }
//        if (email == null || email.trim().length() < 1) {
//            return Result.error("电子邮箱不能为空");
//        }
        if (type == null) {
            return Result.error("稿件类型不能为空");
        }
        if( publishType == null){
            return Result.error("发布类型不能为空");
        }
        if (channelId == null) {
            return Result.error("预发布频道不能为空");
        }
        if(note!=null&&note.trim().length()>300){
            return Result.error("发布说明最多200个字");
        }
        Contribute contribute = new Contribute();
        contribute.setContributor(contributor);
        contribute.setTel(tel);
        contribute.setEmail(email);
        contribute.setContact(contact);
        contribute.setPostcode(postcode);
        contribute.setTitle(title);
        contribute.setType(type);
        contribute.setChannelId(channelId);
        contribute.setNote(note);
        contribute.setUserId(userId);
        contribute.setPublishType(publishType);
        contribute.setRelativeUrl(relativeUrl);
        if(prepublishDate!=null){
            log.info("请求参数prepublishDate={}", prepublishDate);
            contribute.setPrepublishDate(DateUtil.stringtoDate(prepublishDate, "yyyy-MM-dd"));
        }
        contribute.setCompany(company);
        return contributeService.saveContribute(contribute, files);
    }


    @RequestMapping("listContributeType")
    public Result listContributeType(@CurrentUser Users user) {
        List<ContributeType> contributeTypeList = contributeService.listContributeType();
        return Result.success(contributeTypeList);
    }

    @RequestMapping("listContributeChannel")
    public Result listContributeChannel(@CurrentUser Users user) {
        List<ContributeChannel> contributeChannelList = contributeService.listContributeChannel();
        return Result.success(contributeChannelList);
    }

    @RequestMapping("listContributePublishType")
    public Result listContributePublishType(@CurrentUser Users user) {
        List<ContributePublishType> contributePublishTypeList = contributeService.listContributePublishType();
        return Result.success(contributePublishTypeList);
    }

    @RequestMapping("listMyContribute")
    public Result listMyContribute(@RequestBody ContributeQueryParam param, @CurrentUser Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }
        return contributeService.listMyContribute(param, user.getUserid());
    }

    @RequestMapping("getContributeDetail")
    public Result getContributeDetail(@RequestBody ContributeParam param, @CurrentUser Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }
        return contributeService.getContributeDetail(param.getId(), user);
    }

    @RequestMapping("saveLesson")
    public Result saveLesson(@RequestParam(value = "files", required = false) MultipartFile[] files, @RequestParam(value = "name", required = false) String name,
                             @RequestParam(value = "spanId", required = false) Integer spanId, @RequestParam(value = "age", required = false) Integer age,
                             @RequestParam(value = "gender", required = false) Integer gender, @RequestParam(value = "resume", required = false) String resume,
                             @RequestParam(value = "image", required = false) MultipartFile image, @RequestParam(value = "lessonObjects", required = false) Integer[] lessonObjects,
                             @RequestParam(value = "tel", required = false) String tel, @RequestParam(value = "weixin", required = false) String weixin,
                             @RequestParam(value = "email", required = false) String email,
                             @CurrentUser Users user) {
        String userId = user.getUserid();
//        String userId = "634931e94b8d4730ab250166e9d51d40";
        if (userId == null) {
            return Result.error("登录用户id不能为空", "登录用户id不能为空");
        }
        if (name == null || name.trim().length() < 1) {
            return Result.error("姓名不能为空", "姓名不能为空");
        }
        if(name.length()>16){
            return Result.error("姓名不能超过16个字", "姓名不能超过16个字");
        }
        if (spanId == null){
            return Result.error("请选择政府采购从业时间", "请选择政府采购从业时间");
        }
        if (age == null){
            return Result.error("请输入年龄", "请输入年龄");
        }
        if(age.intValue()>100){
            return Result.error("年龄不能超过100", "年龄功能超过100");
        }
        if (resume == null){
            return Result.error("履历简介不能为空", "履历简介不能为空");
        }
        if(resume.length()>300){
            return Result.error("履历简介不能超过300字", "履历简介不能超过300字");
        }
        if(weixin!=null && weixin.length()>20){
            return Result.error("微信名不能超过20个字", "微信名不能超过20个字");
        }
        if(email!=null && email.length()>50){
            return Result.error("电子邮箱不能超过50个字", "电子邮箱不能超过50个字");
        }
        if (files == null || files.length < 1) {
            return Result.error("投稿附件不能为空");
        }
        Lesson lesson = new Lesson();
        lesson.setUserId(userId);
        lesson.setName(name);
        lesson.setSpanId(spanId);
        lesson.setAge(age);
        lesson.setGender(gender);
        lesson.setResume(resume);
        String strLessonObjects = StringUtils.join(lessonObjects, ",");
        lesson.setLessonObjects(strLessonObjects);
        lesson.setTel(tel);
        lesson.setWeixin(weixin);
        lesson.setEmail(email);
        return lessonService.saveLesson(lesson, files, image);
    }

    @RequestMapping("listLessonObject")
    public Result listLessonObject(@CurrentUser Users user) {
        List<LessonObject> lessonObjectList = lessonService.listLessonObject();
        return Result.success(lessonObjectList);
    }

    @RequestMapping("listLessonSpan")
    public Result listLessonSpan(@CurrentUser Users user) {
        List<LessonSpan> lessonSpanList = lessonService.listLessonSpan();
        return Result.success(lessonSpanList);
    }

    @RequestMapping("getContributeConfig")
    public Result getContributeConfig(){
        return contributeService.getContributeConfig();
    }

    @RequestMapping("getLessonConfig")
    public Result getLessonConfig(){
        return lessonService.getLessonConfig();
    }

}
